/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.az.ducbt.cliphot;

import javax.microedition.rms.RecordStore;

/**
 *
 * @author Administrator
 */
public class ReadWriteRec {

    private RecordStore rs = null;
    static final String REC_STORE = "COUNT_SMS_ID";

    public ReadWriteRec() {
    }
    
    public void openRecStore() {
        try {
            rs = RecordStore.openRecordStore(REC_STORE, true);
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void closeRecStore() {
        try {
            rs.closeRecordStore();
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public void deleteRecStore() {
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore(REC_STORE);
                System.out.println("Xóa data!");
            } catch (Exception e) {
                db(e.toString());
            }
        }
    }

    public void writeRecord(String str) {
        byte[] rec = str.getBytes();

        try {
            rs.addRecord(rec, 0, rec.length);
        } catch (Exception e) {
            db(e.toString());
        }
    }

    public int readRecords() {
        int count = 0;
        try {
            byte[] recData = new byte[5];
            int len;

            for (int i = 1; i <= rs.getNumRecords(); i++) {
                if (rs.getRecordSize(i) > recData.length) {
                    recData = new byte[rs.getRecordSize(i)];
                }
                len = rs.getRecord(i, recData, 0);
                String readRec = new String(recData, 0, len);
                //System.out.println("Count SMS: " + i + ": " + readRec);
                //System.out.println("------------------------------");
                count = Integer.parseInt(readRec);
            }
            //System.out.println("Count SMS: " +count);
            return count;
        } catch (Exception e) {
            db(e.toString());
            return count;
        }
    }

    private void db(String str) {
        System.err.println("Msg: " + str);
    }
}
